<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :nav="[{ name: '首页' }, { name: '全部商品' }]"></Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="options1Num == index ? 'active' : ''"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Click(item, index)"
          >
            {{ item.txt }}
          </span>
          <!-- <span>我还可以兑换</span>
          <span>0-500个</span>
          <span>500-1000个</span>
          <span>1000-1500个</span>
          <span>1500-2500个</span> -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="options2Num == index ? 'active' : ''"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Click(item, index)"
            >{{ item.txt }}</span
          >
          <!-- <span>实物礼品</span>
          <span>虚拟礼品</span> -->
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <p style="text-align: center; margin-top: 20px">
        {{ isReachBottom ? "已经没有数据了" : "正在加载... ..." }}
      </p>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodListAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      // 排序的数组  (因为要请求带参数min和max所以每一项都有这两个数据)
      options1: [
        { min: 0, max: 0, txt: "全部" },
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 排序的当前样式
      options1Num: 0,
      // 分类的数组
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实物礼品" },
        { type: 2, txt: "虚拟礼品" },
      ],
      // 分类的当前样式
      options2Num: 0,
      // 栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // ( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      // 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词
      keyword: "",
      // 产品列表原版（包含所有的）
      goodsList: [],
      // 产品列表展示
      goodsListShow: [],
      // 下拉加载的页数
      page: 1,
      // 每页条数
      size: 8,
      // false 表示没有正在加载
      isLoading: false,

      isReachBottom: false,
    };
  },
  methods: {
    options1Click(item, index) {
      // 处理当前样式
      this.options1Num = index;
      // 修改最小值，和最大值，为当前点击的这一项
      this.min = item.min;
      this.max = item.max;
      this.getGoodList();
    },
    options2Click(item, index) {
      // 处理当前样式
      this.options2Num = index;
      // 修改type值
      this.type = item.type;
      this.getGoodList();
    },
    getGoodList() {
      GoodListAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      }).then((res) => {
        console.log(res);
        this.goodsList = res.data;

        this.goodsListShow = this.goodsList.filter(
          (item, index) => index < this.size
        );
      });
    },
    scrollFn() {
      // 滚动就执行这里的代码
      console.log(getClientHeight() + getScrollTop() == getScrollHeight() + 1);
      // console.log("页面正在滚动");
      // 如果滚动到底部的时候，
      // if (到底部了) {
      // if (窗口高度+scrollTop>=页面文档高度-20) {
      if (getClientHeight() + getScrollTop() >= getScrollHeight() - 20) {
        if (this.goodsListShow.length >= this.goodsList.length) {
          // 没有数据了
          this.isReachBottom = true;
          return;
        }
        // 需要为false才能进行加载
        if (!this.isLoading) {
          // this.isLoading避免了重复触发这个到底了加载数据事件
          this.page++;
          this.isLoading = true;
          setTimeout(() => {
            // 往goodsListShow这个数组去push下一页的数据
            // 从goodsList数组中去  this.page页的数据 push到goodsListShow
            for (
              var i = this.size * (this.page - 1);
              i < this.size * this.page;
              i++
            ) {
              //this.goodsList[i]必须有这个值，才能push到展示的数组里面去
              this.goodsList[i]
                ? this.goodsListShow.push(this.goodsList[i])
                : "";
            }
            this.isLoading = false;
          }, 500);
        }
      }
    },
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  components: {
    Crumb,
    List,
  },
  created() {
    this.keyword = this.$route.query.keyword || "";
    this.getGoodList();
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.$router.go(0);
        }
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>